const app = getApp()
Page({


    data: {
        detailContent:"",
        description:"",
        detailId:"",
        modify:false
    },
    onLoad(query) {
        const self = this
        const id = query.id
        app.http({
            url:`https://api.yizhanketang.cn/api/v1/todos/${id}`
        }).then(res=>{
            self.setData({
                detailContent: res.data.data.content,
                description: res.data.data.description,
                detailId:res.data.data.objectId
            })
        })
    },
    submitOrFix(){
        if(!this.data.modify){
            this.setData({
                modify:!this.data.modify
            })
        }else{
           app.http({
               method:"POST",
               url:`https://api.yizhanketang.cn/api/v1/todos/${this.data.detailId}`,
               data:{
                   content:this.data.detailContent,
                   description:this.data.description
               }
           }).then(res =>{
               wx.navigateBack({
                   delta: 2
               })
           })
        }
    },
    deleteItem(){
        const self = this
        wx.showModal({
            title: '警告',
            content: '您确定要删除此项数据吗？',
            success (res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '加载中...',
                    })
                    wx.request({
                        method:"POST",
                        url:`https://api.yizhanketang.cn/api/v1/todos/${self.data.detailId}/delete`,
                        success(res) {
                            wx.navigateBack({
                                delta: 1
                            })
                        }
                    })
                } else if (res.cancel) {
                    return
                }
            }
        })
    },
})